package cn.jiangxiong.pethome.system.shop.controller;

import cn.jiangxiong.pethome.constants.BaseConstants;
import cn.jiangxiong.pethome.system.shop.domain.Shop;
import cn.jiangxiong.pethome.system.shop.domain.ShopAuditLog;
import cn.jiangxiong.pethome.system.shop.domain.ShopRo;
import cn.jiangxiong.pethome.system.shop.query.ShopQuery;
import cn.jiangxiong.pethome.system.shop.service.IShopAuditLogService;
import cn.jiangxiong.pethome.system.shop.service.IShopService;
import cn.jiangxiong.pethome.utils.AjaxResult;
import cn.jiangxiong.pethome.utils.BusinessException;
import cn.jiangxiong.pethome.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    private IShopAuditLogService shopAuditLogService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常请稍后再试！！！");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody ShopQuery query)
    {
        try {
            List<Shop> byQuery = shopService.findByQuery(query);
            Long count = shopService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
       
    }

    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
            shopAuditLogService.add(shopAuditLog);
            return AjaxResult.success();
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常请稍后再试！！！");
        }
    }


    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
            shopAuditLogService.add(shopAuditLog);
            return AjaxResult.success();
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常请稍后再试！！！");
        }
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,"店铺数据","店铺数据表格",Shop.class,"导出的店铺数据.xls",response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        //导入数据需要校验

        shops.forEach(System.out::println);
    }

    @GetMapping("/countByDate")
    public AjaxResult countByDate(){
        try {
            List<ShopRo> shopRos = shopService.conuntByDate();
            return AjaxResult.success().setData(shopRos);
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常请稍后再试！！！");
        }
    }
    @GetMapping("/activation/{shopid}")
    public String activation(@PathVariable("shopid")Long shopid){
        shopService.activation(shopid);
        return "激活成功！";
    }

}
